#!/bin/bash

case $(uname -m) in
    x86_64)  
        CPU_ARCH="x86"
    ;;
    aarch64) 
        CPU_ARCH="arm"
    ;;
esac



user_ps1_text='export PS1="\[\e[92;1m\]\u@\h \[\e[0m\]\[\e[33m\]\w \[\e[35;1m\][\t]\[\e[0m\]\n\[\e[36m\]$\[\e[0m\] "'
user_vim_text="set number\nset autoindent\nset ts=4\nset pastetoggle=<F8>\nautocmd FileType python setlocal et sta sw=4 sts=4" 


add_line()
{
    if (! grep -Fxq "$1" "$2"); then
        echo $1 >> $2;
    else
        echo "add_line(): \"$1\" already in $2.";
    fi
}


echo_options()
{
    echo -e "\t$1\t\t$2"
}


help()
{
    echo -e "usage:"
    echo -e "./install-common.sh [options...]"
    echo -e "./install-common.sh --common"
    echo -e "./install-common.sh --docker"
    echo -e "./install-common.sh --nvidia_install"
    echo_options "--common" "install common software, setup zsh, path, ssh and ssr"
    echo_options "--zsh" "install oh-my-zsh and plugins with gitee mirror"
    echo_options "--dbsys" "set time for linux and win"
    echo_options "--nvidia_install" "pre process of nvidia-drivers install"
    echo_options "--nvidia_uninstall" "uninstall nvidia drivers"
    echo_options "--nvidia_help" "some url for cuda nvidia-docker"
    echo_options "--docker" "install docker"
    echo_options "--nx" "install nx software"
}



install_common()
{
    # sudo apt purge firefox -y
    # sudo apt install chromium-browser -y
    
    sudo apt install -y \
        vim exfat-utils net-tools git dos2unix mlocate curl \
        openssh-server openssl libssl-dev ca-certificates \
        zsh terminator dconf-editor \
        libfmt-dev \
    ;

    gsettings set org.gnome.desktop.default-applications.terminal exec /usr/bin/gnome-terminal
    gsettings set org.gnome.desktop.default-applications.terminal exec-arg "-x"

    echo $user_ps1_text >> ~/.bashrc
    echo -e $user_vim_text > ~/.vimrc
    
    git config --global core.editor vim
    git config --global user.email "653538096@qq.com"
    git config --global user.name "BrightSoulXYHY"

    # python3
    sudo apt install python3-pip -y
    pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
    pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple


    # serial manger
    sudo usermod -a -G dialout $USER
    sudo apt remove modemmanager -y

    # some keys
    dconf load /org/gnome/settings-daemon/plugins/media-keys/ < res/bs_key.txt

    mkdir ~/Programing
    mkdir ~/SoftWare
}

set_up_ssh()
{
    if [ ! -d "~/.ssh" ];then
        mkdir -p ~/.ssh
        wget https://offical-account-bs.oss-cn-beijing.aliyuncs.com/gitee/keys_pub.txt -O ~/.ssh/authorized_keys
    fi
}

set_up_ssr()
{
    add_line 'alias ssr-set="export all_proxy=http://B760-13KF:10800;"' ~/.zshrc
    add_line 'alias ssr-unset="unset all_proxy"' ~/.zshrc
    add_line 'alias ssr-test="curl google.com"' ~/.zshrc  
}


set_up_path()
{
    add_line 'export PATH=/usr/local/cuda/bin:$PATH' ~/.zshrc
    add_line 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' ~/.zshrc
}


install_zsh_and_plugin()
{
    wget https://gitee.com/mirrors/ohmyzsh/raw/master/tools/install.sh -O ./install-zsh.sh
    REMOTE=https://gitee.com/mirrors/ohmyzsh.git sh ./install-zsh.sh
    rm -rf ./install-zsh.sh
    zsh ./install-zsh_plugins.sh
}


install_dbsys()
{
    sudo apt install -y pigz gparted
    sudo apt install -y vlc simplescreenrecorder
    echo "set time consist with Win10"
    sudo timedatectl set-local-rtc 1
    sudo hwclock --localtime --systohc
}


nvidia_install()
{
    nvidia_text="blacklist nouveau\noptions nouveau modeset=0"
    
    sudo touch /etc/modprobe.d/blacklist-nouveau.conf
    echo -e ${nvidia_text} | sudo tee /etc/modprobe.d/blacklist-nouveau.conf
    sudo update-initramfs -u
}

nvidia_uninstall()
{
    sudo apt-get --purge remove nvidia* -y
    sudo apt-get --purge remove "*cublas*" "cuda*" -y
    sudo apt autoremove
}



nvidia_help()
{
    echo -e "cuda:\t\thttps://developer.nvidia.com/cuda-downloads"
    echo -e "cudnn:\t\thttps://developer.nvidia.com/cudnn-downloads"
}



install_docker()
{
    # uninstall docker  
    for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do 
        sudo apt-get remove $pkg; 
    done
    sudo apt 
    # install docker  
    curl -fsSL https://get.docker.com -o get-docker.sh
    sudo sh ./get-docker.sh --mirror Aliyun
    rm -rf get-docker.sh
    sudo apt install -y uidmap
    #dockerd-rootless-setuptool.sh install
}


# for jetson xavite nx
# ==========================================================
install_NX()
{
    # VNC
    # sudo apt install tightvncserver vino xrdp xbase-clients -y
    # sudo systemctl start xrdp

    # gsettings set org.gnome.Vino require-encryption false
    # # gsettings set org.gnome.Vino enabled true
    # gsettings set org.gnome.Vino prompt-enabled false
    # gsettings set org.gnome.Vino authentication-methods "['vnc']"
    # gsettings set org.gnome.Vino vnc-password $(echo -n '123456'|base64)

    sudo -H pip3 install jetson-stats
}




# archice 
# ==========================================================

install_cmake()
{
    wget ${CMAKE_URL} -O cmke.tar.gz
    sudo tar -xpzf cmke.tar.gz -C /opt
    CMAKE_BASENAME=`basename ${CMAKE_FILE} .tar.gz`
    sudo mv /opt/${CMAKE_BASENAME} /opt/cmake
    rm -rf cmke.tar.gz
    add_line 'export PATH=/opt/cmake/bin:$PATH' ~/.zshrc
}

install_deb()
{
    # install vscode   
    wget $VSCODE_DEB -O code.deb
    sudo dpkg -i code.deb
    cp "res/vs code.sh" ~/.local/share/nautilus/scripts/
    rm -rf code.deb

    # install nomachine 
    wget ${NM_DEB} -O tmp_nm.deb
    sudo dpkg -i tmp_nm.deb
    rm -rf tmp_nm.deb
}

# ==========================================================


while [ -n "$1" ]
do
    case "$1" in
        --help) 
            help
            ;;
        --common) 
            install_common
            install_zsh_and_plugin
            set_up_path
            set_up_ssh
            set_up_ssr
            ;;
        --zsh) 
            install_zsh_and_plugin
            ;;
        --dbsys) 
            install_dbsys
            ;;
        --nvidia_install) 
            nvidia_install
            ;;
        --nvidia_uninstall) 
            nvidia_uninstall
            ;;
        --docker) 
            install_docker
            ;;
        --nx) 
            install_NX
            ;;
        *) echo "[-] Parameters of illegal !!!"
            help
            exit 1
            ;;
    esac
    shift
done



